package com.sjgame.base.controller;

import com.sjgame.base.common.JsonResult;
import com.sjgame.base.entity.Resource;
import com.sjgame.base.service.IResourceService;
import com.sjgame.base.vo.ZtreeView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * create by zcl
 */
@Controller
@RequestMapping("/resource")
public class ResourceController extends BaseController {
    @Autowired
    private IResourceService resourceService;

    @RequestMapping("/tree/{resourceId}")
    @ResponseBody
    public List<ZtreeView> tree(@PathVariable Integer resourceId) {
        List<ZtreeView> list = resourceService.tree(resourceId);
        return list;
    }

    @RequestMapping("/index")
    public String index(ModelMap map) {

        List<Resource> list = resourceService.findAllByParentId(null);
        if (list != null && list.size() > 0) {
            List<Resource> list2 = new ArrayList<>();
            for (Resource r : list) {
                if (r.getChilds() != null && r.getChilds().size() > 0) {
                    dealResource(list2, r);
                } else {
                    list2.add(r);
                }
            }
            map.put("list", list2);
        } else {
            map.put("list", list);
        }
        return "resource/index";
    }

    private void dealResource(List s, Resource r) {
        s.add(r);
        if (r.getChilds() != null && r.getChilds().size() > 0) {
            for (Resource r2 : r.getChilds()) {
                if (r2.getChilds() != null && r2.getChilds().size() > 0) {
                    dealResource(s, r2);
                } else {
                    s.add(r2);
                }
            }
        }
    }

//    @RequestMapping("/index")
//    public String index(ModelMap map) {
//        return redirect("index/0");
//    }
//
//    @RequestMapping("/index/{id}")
//    public String index(ModelMap map,@PathVariable Integer id) {
//        if(id == 0){
//            id = null;
//        }
//        List<Resource> list = resourceService.findAllByParentId(id);
//        map.put("list", list);
//        return "resource/index";
//    }
//    @RequestMapping("/index2/{id}")
//    public JsonResult index2(ModelMap map,@PathVariable Integer id) {
//        if(id == 0){
//            id = null;
//        }
//        List<Resource> list = resourceService.findAllByParentId(id);
//        map.put("list", list);
//
//       return JsonResult.success(list);
//    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap map) {
//        List<Resource> list = resourceService.findAll();
//        map.put("list", list);
        List<Resource> list = resourceService.findAllByParentId(null);
        if (list != null && list.size() > 0) {
            List<Resource> list2 = new ArrayList<>();
            for (Resource r : list) {
                if (r.getChilds() != null && r.getChilds().size() > 0) {
                    dealResource(list2, r);
                } else {
                    list2.add(r);
                }
            }
            map.put("list", list2);
        } else {
            map.put("list", list);
        }
        return "resource/form";
    }


    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable Integer id, ModelMap map) {
        Resource resource = resourceService.find(id);
        map.put("resource", resource);

        List<Resource> list = resourceService.findAll();
        map.put("list", list);
        return "resource/form";
    }

    @RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult editResource(Resource resource, Integer parentId, ModelMap map) {
        try {
            if (parentId != null) {
                Resource parent = new Resource();
                parent.setId(parentId);
                resource.setParent(parent);
            }
            resourceService.saveOrUpdate(resource);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult deleteResource(@PathVariable Integer id, ModelMap map) {
        try {
            resourceService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.failure("删除失败");
        }
        return JsonResult.success();
    }

    @RequestMapping(value = "/checkSourceKey", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult checkSourceKey(String sourceKey, ModelMap map) {
        try {
            Resource r= resourceService.findBySourceKey(sourceKey);
            if(r!=null)return JsonResult.failure("资源key已存在");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }
}
